package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.xjy.common.core.page.PagePlus;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.common.utils.PageUtils;
import com.xjy.system.domain.ReportMaterial;
import com.xjy.system.domain.bo.ReportMaterialAddBo;
import com.xjy.system.domain.bo.ReportMaterialEditBo;
import com.xjy.system.domain.bo.ReportMaterialQueryBo;
import com.xjy.system.domain.vo.ReportMaterialVo;
import com.xjy.system.mapper.ReportMaterialMapper;
import com.xjy.system.service.IReportMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 物料数据汇总报表Service业务层处理
 *
 * @author zc
 * @date 2022-03-21
 */
@Service
public class ReportMaterialServiceImpl extends ServicePlusImpl<ReportMaterialMapper, ReportMaterial> implements IReportMaterialService {

	@Autowired
	ReportMaterialMapper reportMaterialMapper;

	@Override
	public ReportMaterialVo queryById(String mindate) {
		return getVoById(mindate, ReportMaterialVo.class);
	}

	@Override
	public TableDataInfo<ReportMaterialVo> queryPageList(ReportMaterialQueryBo bo) {
		ArrayList<ReportMaterial> list = reportMaterialMapper.queryList(bo);
		return PageUtils.getPageInfo(list);
	}

	@Override
	public List<ReportMaterialVo> queryList(ReportMaterialQueryBo bo) {
		return listVo(buildQueryWrapper(bo), ReportMaterialVo.class);
	}

	private LambdaQueryWrapper<ReportMaterial> buildQueryWrapper(ReportMaterialQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<ReportMaterial> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getProjId() != null, ReportMaterial::getProjId, bo.getProjId());
		lqw.like(StrUtil.isNotBlank(bo.getProjName()), ReportMaterial::getProjName, bo.getProjName());
		lqw.eq(StrUtil.isNotBlank(bo.getMaterialId()), ReportMaterial::getMaterialId, bo.getMaterialId());
		lqw.like(StrUtil.isNotBlank(bo.getName()), ReportMaterial::getName, bo.getName());
		lqw.eq(StrUtil.isNotBlank(bo.getUnit()), ReportMaterial::getUnit, bo.getUnit());
		lqw.eq(bo.getBeginyue() != null, ReportMaterial::getBeginyue, bo.getBeginyue());
		lqw.eq(bo.getRuku() != null, ReportMaterial::getRuku, bo.getRuku());
		lqw.eq(bo.getJiechu() != null, ReportMaterial::getJiechu, bo.getJiechu());
		lqw.eq(bo.getGuihuan() != null, ReportMaterial::getGuihuan, bo.getGuihuan());
		lqw.eq(bo.getBaosun() != null, ReportMaterial::getBaosun, bo.getBaosun());
		lqw.eq(bo.getShoumai() != null, ReportMaterial::getShoumai, bo.getShoumai());
		lqw.eq(bo.getEndyue() != null, ReportMaterial::getEndyue, bo.getEndyue());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(ReportMaterialAddBo bo) {
		ReportMaterial add = BeanUtil.toBean(bo, ReportMaterial.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(ReportMaterialEditBo bo) {
		ReportMaterial update = BeanUtil.toBean(bo, ReportMaterial.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(ReportMaterial entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
